import time
import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil


class TestColocationManagement:

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/1_get_colocation_status.yaml'))
    def test_get_colocation_status(self, colocationinfo, get_env_info):
        # 查询集群中混部统计信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/2_get_colocation_node_info.yaml'))
    def test_get_colocation_node_info(self, colocationinfo, get_env_info):
        # 查询混部节点信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/2_get_non_colocation_node_info.yaml'))
    def test_get_non_colocation_node_info(self, colocationinfo, get_env_info):
        # 查询非混部节点信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if len(req.json()['data']) > 1:
            globals()['nodeuid'] = req.json()['data'][1]['uid']
            globals()['nodename'] = req.json()['data'][1]['name']
            globals()['addresses'] = req.json()['data'][1]['addresses']
            globals()['osImage'] = req.json()['data'][1]['osImage']
        else:
            print('There has no node to colocation!')
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/2_non_colocation_node_set_to_colocation_node.yaml'))
    def test_non_colocation_node_set_to_colocation_node(self, colocationinfo, get_env_info):
        # 非混部节点添加为混部节点
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        if globals()['nodename'] is not None:
            colocationinfo['request']['data'][0]['uid'] = globals()['nodeuid']
            colocationinfo['request']['data'][0]['name'] = globals()['nodename']
            colocationinfo['request']['data'][0]['addresses'] = globals()['addresses']
            colocationinfo['request']['data'][0]['osImage'] = globals()['osImage']
            colocationinfo['request']['data'][0]['key'] = globals()['nodeuid']
            data = colocationinfo['request']['data']
            req = RequestUtil().send_request(method, url, json=data)
            assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']
        else:
            print('There has no node to colocation!')

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/3_get_colocation_pod_info.yaml'))
    def test_get_colocation_pod_info(self, colocationinfo, get_env_info):
        # 查询混部pod信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/4_get_colocation_deployment_info.yaml'))
    def test_get_colocation_deployment_info(self, colocationinfo, get_env_info):
        # 查询混部deployment信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/4_get_non_colocation_deployment_info.yaml'))
    def test_get_non_colocation_deployment_info(self, colocationinfo, get_env_info):
        # 查询非混部deployment信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if  req.json()['data'] is not None:
            globals()['deploymentuid'] = req.json()['data'][0]['uid']
            globals()['name'] = req.json()['data'][0]['name']
            globals()['namespace'] = req.json()['data'][0]['namespace']
            globals()['creationTimestamp'] = req.json()['data'][0]['creationTimestamp']
            globals()['labels'] = req.json()['data'][0]['labels']
            assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']
        else:
            print('There has no deployment to colocation!')

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/4_non_colocation_deployment_set_to_colocation_deployment.yaml'))
    def test_non_colocation_deployment_set_to_colocation_deployment(self, colocationinfo, get_env_info):
        # 非混部deployment添加为混部deployment
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        colocationinfo['request']['data'][0]['uid'] = globals()['deploymentuid']
        colocationinfo['request']['data'][0]['name'] = globals()['name']
        colocationinfo['request']['data'][0]['namespace'] = globals()['namespace']
        colocationinfo['request']['data'][0]['creationTimestamp'] = globals()['creationTimestamp']
        colocationinfo['request']['data'][0]['labels'] = globals()['labels']
        colocationinfo['request']['data'][0]['key'] = globals()['deploymentuid']
        data = colocationinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/4_colocation_deployment_set_to_non_colocation_deployment.yaml'))
    def test_colocation_deployment_set_to_non_colocation_deployment(self, colocationinfo, get_env_info):
        # 混部deployment设置为混部deployment
        time.sleep(5)
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url'].replace('{uid}',globals()['deploymentuid'])
        method = colocationinfo['request']['method']
        data = colocationinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/5_get_colocation_statefulset_info.yaml'))
    def test_get_colocation_statefulset_info(self, colocationinfo, get_env_info):
        # 查询混部statefulset信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/5_get_non_colocation_statefulset_info.yaml'))
    def test_get_non_colocation_statefulset_info(self, colocationinfo, get_env_info):
        # 查询非混部statefulset信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if req.json()['data'] is not None:
            globals()['statefulsetuid'] = req.json()['data'][0]['uid']
            globals()['name'] = req.json()['data'][0]['name']
            globals()['namespace'] = req.json()['data'][0]['namespace']
            globals()['creationTimestamp'] = req.json()['data'][0]['creationTimestamp']
            globals()['labels'] = req.json()['data'][0]['labels']
            assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']
        else:
            print('There has no statefulset to colocation!')

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/5_non_colocation_statefulset_set_to_colocation_statefulset.yaml'))
    def test_non_colocation_statefulset_set_to_colocation_statefulset(self, colocationinfo, get_env_info):
        # 非混部statefulset添加为混部statefulset
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        colocationinfo['request']['data'][0]['uid'] = globals()['statefulsetuid']
        colocationinfo['request']['data'][0]['name'] = globals()['name']
        colocationinfo['request']['data'][0]['namespace'] = globals()['namespace']
        colocationinfo['request']['data'][0]['creationTimestamp'] = globals()['creationTimestamp']
        colocationinfo['request']['data'][0]['labels'] = globals()['labels']
        colocationinfo['request']['data'][0]['key'] = globals()['statefulsetuid']
        data = colocationinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/5_colocation_statefulset_set_to_non_colocation_statefulset.yaml'))
    def test_colocation_statefulset_set_to_non_colocation_statefulset(self, colocationinfo, get_env_info):
        # 混部statefulset设置为混部statefulset
        time.sleep(5)
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url'].replace('{uid}',globals()['statefulsetuid'])
        method = colocationinfo['request']['method']
        data = colocationinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/6_get_colocation_job_info.yaml'))
    def test_get_colocation_job_info(self, colocationinfo, get_env_info):
        # 查询混部job信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/7_get_colocation_cronjob_info.yaml'))
    def test_get_colocation_cronjob_info(self, colocationinfo, get_env_info):
        # 查询混部cronjob信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    globals()['flag'] = 0
    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/7_get_non_colocation_cronjob_info.yaml'))
    def test_get_non_colocation_cronjob_info(self, colocationinfo, get_env_info):
        # 查询非混部cronjob信息
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)

        if req.json()['data'] is not None:
            globals()['flag'] = 1
            globals()['cronjobuid'] = req.json()['data'][0]['uid']
            globals()['name'] = req.json()['data'][0]['name']
            globals()['namespace'] = req.json()['data'][0]['namespace']
            globals()['creationTimestamp'] = req.json()['data'][0]['creationTimestamp']
            globals()['labels'] = req.json()['data'][0]['labels']
            assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']
        else:
            print('There has no cronjob to colocation!')

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/7_non_colocation_cronjob_set_to_colocation_cronjob.yaml'))
    def test_non_colocation_cronjob_set_to_colocation_cronjob(self, colocationinfo, get_env_info):
        # 非混部cronjob添加为混部cronjob
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        if globals()['flag'] == 1:
            colocationinfo['request']['data'][0]['uid'] = globals()['cronjobuid']
            colocationinfo['request']['data'][0]['name'] = globals()['name']
            colocationinfo['request']['data'][0]['namespace'] = globals()['namespace']
            colocationinfo['request']['data'][0]['creationTimestamp'] = globals()['creationTimestamp']
            colocationinfo['request']['data'][0]['labels'] = globals()['labels']
            colocationinfo['request']['data'][0]['key'] = globals()['cronjobuid']
            data = colocationinfo['request']['data']
            req = RequestUtil().send_request(method, url, json=data)
            assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']
        else:
            print('There has no cronjob to colocation!')

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/7_colocation_cronjob_set_to_non_colocation_cronjob.yaml'))
    def test_colocation_cronjob_set_to_non_colocation_cronjob(self, colocationinfo, get_env_info):
        # 混部cronjob设置为混部cronjob
        time.sleep(5)
        baseUrl = get_env_info
        if globals()['flag'] == 1:
            url = baseUrl + colocationinfo['request']['url'].replace('{uid}',globals()['cronjobuid'])
            method = colocationinfo['request']['method']
            data = colocationinfo['request']['data']
            req = RequestUtil().send_request(method, url, json=data)
            assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']
        else:
            print('There has no cronjob to colocation!')

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/2_colocation_node_set_to_non_colocation_node.yaml'))
    def test_colocation_node_set_to_non_colocation_node(self, colocationinfo, get_env_info):
        # 混部节点设置为混部节点
        baseUrl = get_env_info
        if globals()['nodename'] is not None:
            url = baseUrl + colocationinfo['request']['url'].replace('{name}',globals()['nodename'])
            method = colocationinfo['request']['method']
            data = colocationinfo['request']['data']
            req = RequestUtil().send_request(method, url, json=data)
            assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']
        else:
            print('There has no node to colocation!')

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/8_get_colocation_rules.yaml'))
    def test_get_colocation_rules(self, colocationinfo, get_env_info):
        # 查询混部规则
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/8_create_colocation_rules.yaml'))
    def test_create_colocation_rules(self, colocationinfo, get_env_info):
        # 创建混部规则
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        data = colocationinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('colocationinfo', YamlUtil().read_testcase_yaml('v24.09/colocation/8_delete_colocation_rules.yaml'))
    def test_delete_colocation_rules(self, colocationinfo, get_env_info):
        # 删除混部规则
        baseUrl = get_env_info
        url = baseUrl + colocationinfo['request']['url']
        method = colocationinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == colocationinfo['validate'][0]['equals']['status_code']